#!/bin/bash
### BEGIN INIT INFO
# Provides:          pocp-landingpage
# Required-Start:    $local_fs $network $syslog postgresql
# Required-Stop:     $local_fs $network $syslog postgresql
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/stop opencaptiveportal landingpage
### END INIT INFO

# This init.d Script is to start the python daemon, which answers the fastcgi
# requests

. /lib/lsb/init-functions

# Only as root executable
if [ $UID != 0 ]; then
  echo "Error: $0 must be executed as user root!"
  exit 2
fi

# Configuration
DESC="Open Captive Portal"
NAME="pocp"
OCPPIDFILE=/var/www/fast_cgi/django.pid
OCPUSER="www-data"

start() {
  # Environment for http server
  mkdir -p           /var/www/fast_cgi/
  chown -R www-data: /var/www/fast_cgi/
  # Logfile for ldap
  mkdir -p        /var/log/pocp
  rm -rf          /var/log/pocp/ldap.debug.log
  touch           /var/log/pocp/ldap.debug.log
  chown www-data: /var/log/pocp/ldap.debug.log
  # Environment for django service
  cd /usr/local/pocp/ || { echo "Could not changedir to /usr/local/pocp/"; exit 9; }
  # Reload iptables
  log_daemon_msg "Reloading iptables" "iptables-restore.py"
  ./shell/django-python.sh ./iptables-restore.py
  if [ "$?" -eq 0 ]; then
    log_end_msg 0
  else
    log_end_msg 1
  fi
  # Start landingpage daemon
  log_daemon_msg "Starting $DESC" "$NAME"
  sudo -u www-data sh -c "DJANGO_SETTINGS_MODULE='pocp.settings' python ./manage.py runfcgi method=prefork \
      socket=/var/www/fast_cgi/mysite.sock \
      pidfile=/var/www/fast_cgi/django.pid"
  # Does not work with start-stop-daemon, why?
  #start-stop-daemon --start --quiet  \
  #  --chdir /usr/local/pocp/  \
  #  --chuid $OCPUSER  \
  #  --pidfile $OCPPIDFILE  \
  #  --exec ./manage.py -- runfcgi method=prefork \   
  #                     socket=/var/www/fast_cgi/mysite.sock  \
  #                     pidfile=$OCPPIDFILE
  sleep 2

  if check_status -q; then
    log_end_msg 0
  else
    log_failure_msg "check syslog for diagnostics."
    log_end_msg 1
    exit 1
  fi
}

check_status() {
  if [ ! -r "$OCPPIDFILE" ]; then
    test "$1" != -v || echo "$NAME is not running."
    return 3
  fi
  if read pid < "$OCPPIDFILE" && ps -p "$pid" > /dev/null 2>&1; then
    test "$1" != -v || echo "$NAME is running."
    return 0
  else
    test "$1" != -v || echo "$NAME is not running but $OCPPIDFILE exists."
    return 1
  fi
}

stop() {
  # Environment for django service
  cd /usr/local/pocp/ || { echo "Could not changedir to /usr/local/pocp/"; exit 9; }
  # Delete GRE tunnels and ip rules
  log_daemon_msg "Deleting old GRE tunnels and IP configuration" "rules-delete.py"
  ./shell/django-python.sh ./rules-delete.py
  if [ "$?" -eq 0 ]; then
    log_end_msg 0
  else
    log_end_msg 1
  fi
  # Stop landingpage
  log_daemon_msg "Stopping $DESC" "$NAME"
  start-stop-daemon --stop --quiet --pidfile $OCPPIDFILE
  log_end_msg $?
  rm -f "$OCPPIDFILE"
}


if [ "$1" == "start" ]; then
  start
elif [ "$1" == "stop" ]; then
  stop
elif [ "$1" == "restart" ]; then
  stop || true
  sleep 1
  start
else 
  echo "Usage: $0 [ start | stop | restart ]"
  exit 9
fi

